<template>
  <div>
    <br>
    <el-form :inline="true">
      <el-form-item label="姓名">
        <el-input v-model="searchMap.bname" placeholder="姓名"></el-input>
      </el-form-item>

      <el-button type="primary" @click="fetchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column prop="id" label="编号" width="200"></el-table-column>
      <el-table-column prop="bname" label="姓名" width="200" sortable></el-table-column>
      <el-table-column prop="contact" label="联系方式" width="200"></el-table-column>
      <el-table-column prop="grade" label="评分" width="180" sortable>
        <template slot-scope="scope">
          <div class="block">
            <el-rate v-model="scope.row.grade"></el-rate>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="photograph" label="照片" width="200">

        <template slot-scope="scope">
          <el-image style="width: 100px; height: 130px;margin: 10px;" :src="scope.row.photograph">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="200"></el-table-column>
      <el-table-column prop="achievement" label="个人成绩" width="200"></el-table-column>
      <el-table-column prop="house_source" label="在售房源" width="200"></el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)"  size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="fetchData"
      @current-change="fetchData"
      :current-page.sync="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="pojo.bname"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="pojo.contact"></el-input>
        </el-form-item>
        <el-form-item label="评分">
          <div class="block">
            <el-rate v-model="pojo.grade"></el-rate>
          </div>
        </el-form-item>
        <el-form-item label="照片">
          <el-image v-model="pojo.photograph" :src="pojo.photograph"></el-image>
          <el-upload
            class="upload-demo"
            action="http://localhost:9999/rent/upload/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="upsuccess"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="地址">
          <v-distpicker province="广东省" city="广州市" area="海珠区" @selected="onSelected"></v-distpicker>
        </el-form-item>
        <el-form-item label="个人成绩">

          <el-input v-model="pojo.achievement"></el-input>
        </el-form-item>
        <el-form-item label="在售房源">
          <el-input v-model="pojo.house_source"></el-input>
        </el-form-item>

        <el-button type="primary" @click="handleSave()">保存</el-button>
        <el-button @click="dialogFormVisible = false">关闭</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import informationApi from '@/api/information'

  export default {
    data() {
      return {
        list: [],
        total: 0, // 总记录数
        currentPage: 1, // 当前页
        pageSize: 10, // 每页大小
        searchMap: {}, // 查询条件
        dialogFormVisible: false, // 编辑窗口是否可见
        pojo: {}, // 编辑表单绑定的实体对象
        cityList: [], // 城市列表
        id: '' // 当前用户编辑的ID
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        informationApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
          this.list = response.data.rows
          this.total = response.data.total
        })
      },
      handleSave() {
        informationApi.update(this.id, this.pojo).then(response => {
          this.$message({
            message: response.message,
            type: (response.flag ? 'success' : 'error')
          })
          if (response.flag) { // 如果成功
            this.fetchData() // 刷新列表
          }
        })
        this.dialogFormVisible = false // 关闭窗口
      },
      handleEdit(id) {
        this.id = id
        this.dialogFormVisible = true // 打开窗口
        if (id !== '') { // 编辑
          informationApi.findById(id).then(response => {
            if (response.flag) {
              this.pojo = response.data
            }
          })
        } else {
          this.pojo = {} // 清空数据
        }
      },
      handleDelete(id) {
        this.$confirm('确定要删除此纪录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          informationApi.deleteById(id).then(response => {
            this.$message({message: response.message, type: (response.flag ? 'success' : 'error')})
            if (response.flag) {
              this.fetchData() // 刷新数据
            }
          })
        })
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      upsuccess(response, file, fileList){
        this.pojo.photograph = "https://1321654.oss-cn-beijing.aliyuncs.com/"+response.src
      },
      onSelected(data) {
          this.pojo.address = data.province.value+'-'+data.city.value+'-'+data.area.value
      }
    }
  }
</script>
